<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 短信API管理
 * Class smsApiController
 * @package Admin\Controller
 */
class SmsApiController extends BaseController {


    private $queryUrl = 'http://api.smsbao.com/query?u=USERNAME&p=PASSWORD';
    static $smsUrl = 'http://api.smsbao.com/sms?u=USERNAME&p=PASSWORD&m=PHONE&c=CONTENT';

    /**
     * 短信API维护 -> formview
     */
    public function detail(){
        //读取短信API
        $token = I("get.token");
        $smsApi = M("sms_api")->where(array("token"=>$token))->find();
        $this->assign("smsApi", $smsApi);

        $request = strtr($this->queryUrl, [
            "USERNAME" => $smsApi['username'],
            "PASSWORD" => md5($smsApi["passwd"]),
        ]);

        $num = file_get_contents($request);
        $idx = strripos($num, ",");
        $this->assign("num", substr($num, $idx+1));

        $this->display();
    }

    static public function SendMsg($tcode, $phone, $params){
        if(C("ENABLE_SHORTMSG")==false){
            return array("state"=>false,"info"=>"短信功能已被管理员禁用");
        }
        $content = "";
        $smsApi = M("sms_api")->where(array("token"=>"default"))->find();
        $where = array("code"=>$tcode);
        $tmpl = M("sms_tpl")->where($where)->find();
        switch($tcode){
            case "1000":
                $content = strtr($tmpl['content'], [
                    "{{no}}" => $params['no'],
                    "{{name}}" => $params['name'],
                    "{{idno}}" => $params['idno'],
                ]);
                break;
            case "2000":
                $content = strtr($tmpl['content'], [
                    "{{no}}" => $params['no'],
                    "{{reason}}" => $params['reason']
                ]);
                break;
            case "3000":
                $content = strtr($tmpl['content'], [
                    "{{house}}" => $params['house'],
                ]);
                break;
            case "4000":
                $content = strtr($tmpl['content'], [
                    "{{season}}" => $params['season'],
                ]);
                break;
            case "5000":
                $content = strtr($tmpl['content'], [
                    "{{year}}" => $params['year'],
                ]);
                break;
            case "6000":
                $content = strtr($tmpl['content'], [
                    "{{year}}" => $params['year'],
                ]);
                break;
            case "7000":
                $content = strtr($tmpl['content'], [
                    "{{name}}" => $params['name'],
                    "{{year}}" => $params['year'],
                ]);
                break;
            default:break;
        }

        $request = strtr(self::$smsUrl, [
            "USERNAME" => $smsApi['username'],
            "PASSWORD" => md5($smsApi["passwd"]),
            "PHONE" => $phone,
            "CONTENT" => $content,
        ]);
        $res = file_get_contents($request);
        if($res=='0'){
            return array("state"=>true);
        }else{
            return array("state"=>false,"info"=>$res);
        }
    }

    /**
     * AJAX方法，通过ID来修改信息
     */
    public function ajax_UpdateInfoById(){
        $data = I("post.data");
        $dataId = I("post.id");

        $res = M("sms_api")->where(array("id"=>$dataId))->save($data);

        if($res){
            $this->success("0k, 200");
        }else{
            $this->error("更新异常，请稍候再试");
        }
    }

}